<template>
  <div id="app">
    <iframe id="if" :src="game_url"  frameborder="0"></iframe>
  </div>
</template>

<script>
//关闭回调,参数必须是同步函数
function setEndTime(fn) {
  return window.onbeforeunload = async function(){
    fn()
    return '1'
  }
}

//获取参数
function GetParam() {  
  var url = window.location.search; //获取url中"?"符后的字串  
  var param = {};  
  if (url.indexOf("?") != -1) {  
      let  str = url.substr(1);  
      let strs = str.split("&");  
      for(var i = 0; i < strs.length; i ++) {  
        param[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
      }  
  }  
  return param;  
}

export default {
  name: 'app',
  data () {
    return {
      game_url:''
    }
  },
  created () {
    let option = GetParam()
    if(!option.hasOwnProperty('uid')){
      this.$alert('链接错误,请重现复制链接',{
        confirmButtonText: '确定',
      })
      return 
    }

    this.game_url = option.game_url

    let dataToSend = {
      uid:option.uid,
      page_game_id:option.page_game_id
    }

    let start_time = 0
    this.axios({
      url: this.env + '/start_game',
      method:'GET'
    }).then((res) => {
      start_time = res.data.data.startTime
    })
    setEndTime(() => {this.$jq.ajax({
      url: this.env + '/end_game',
      method: 'post',
      async: false,
      data:{
        page_game_id:option.page_game_id,
        start_time:start_time
      },
      success: function(res) {
        console.log(res)
      }
    })})
    
    

    setTimeout(() => {
      this.axios({
        url: this.env + '/open_game',
        method:'post',
        data:dataToSend,
      }).then((res) => {
      })
    },120000)
  }
}
</script>

<style>
#app {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
#if{
  width: 100%;
  height: 100%;
}
</style>
